Perl

PHP (or Perl) one line if/then/else statements

August 8th, 2009

If you're toggling something between two states in PHP or Perl it's often handy to use an if/then/else one liner.

In pseudocode this goes like this:

<if this evaluates to TRUE> then <parse this> else <parse this>

All you need to do is replace the "then" with a question mark and the "else" with a colon:

<if this evaluates to TRUE> ? <parse this> : <parse this>

For example:

print  $trueOrFalse ? "you're telling the truth" : "you're lying";

Ignore the print command, it's not part of the if/then/else statement, it's just here to do something with the outcome of that statement.

The expression immediately to the left of the question mark is evaluated. The expression between the question mark and the colon is parsed if the expression evaluates to TRUE, otherwise the expression immediately to the right of the colon is parsed. So in the above example, either "you're telling the truth" or "you're lying" is printed, depending on whether $trueOrFalse is ... you guessed it ... TRUE or FALSE.

But perhaps a more common situation is toggling the value assigned to a variable. For example, toggling between TRUE and FALSE:

$trueOrFalse = $trueOrFalse ? FALSE : TRUE;

Here's a practical example of the use of if/then/else one liners. There's two in this chunk of PHP. The scroll box list below the code is the kind of thing this PHP produces.

<div style="overflow:auto; height:100px; width:300px; border:3px groove #DDD; padding:0">
<?php
    $alternateLine = FALSE;
    while($presidentsArray) {
        print "<div style=\"background-color:";
        print $alternateLine ? "#F5F8F9" : "white";
        print "; padding-bottom: 1px\"> &nbsp; &nbsp; <a href=\"someURL\" title=\"This link goes nowhere\">" .
          $presidentsArray['name'] . "</a></div>";
        $alternateLine = $alternateLine ? FALSE : TRUE;
    }
?>
</div>

Leave a comment

 

Changing your CVS host

February 5th, 2009

What do you do if the host name of your CVS server changes? For example, here’s my case. I checked out a whole lot of CVS modules from the repository while my laptop was on the domain. Now however, thanks to Vista SP1 not playing with an antiquated NT domain, the laptop can’t join the domain so to connect to a server I need to qualify its name with a domain. So, whereas I checked out the modules using the hostname “cvshost”, I now need to use “cvshost.mydomain.co.uk”.

Unfortunately, TortoiseCVS has no way to change the host names for modules you’ve already checked out. WinCVS can, supposedly, do this thanks to a macro. However, WinCVS stubbornly tells me I don’t have Python installed (I do) and therefore won’t let me use macros.

The solution is to just go through all the CVS “Root” files and change the host name. The Root file lives in the CVS directory at each level within a checked out module. This would be a laborious task by hand, but if you have SUA (Microsoft’s Subsystem for UNIX Applications) and Perl installed it’s easy. Just pull up a Korn shell and browse to the directory within which your checked out CVS modules live.

Run the following command.

find . -name 'Root' -print0 | xargs -0 perl -pi -e 's/oldhostname/newhostname/g'

For example, I ran the command:

find . -name 'Root' -print0 | xargs -0 perl -pi -e 's/cvshost/cvshost.mydomain.co.uk/g'

Which changed the Contents of the Root file from:

:pserver:achristie@cvshost:/company/repository

to:

:pserver:achristie@cvshost.mydomain.co.uk:/company/repository

Leave a comment



Setting up the Perl CPAN module installer

October 11th, 2007    2 Comments

perl -MCPAN -e 'install Collection::Module'

is the standard way of installing Perl modules.

The first time you run this you get asked lots of questions. For most of these you can just hit Enter to accept the default. However, you must tell it where to find the modules - i.e. the name of a CPAN mirror site. You can add several, one after the other, so that, if the first one fails, it just tries the next one in the urllist.

You can find a list of URLs at:

http://www.cpan.org/SITES.html

Now common sense might tell you that all you need to do is copy the URL for one of the FTP sites, paste it in and Bob's your uncle. Not so, in some cases, however, because if you're sitting behind a firewall that's fussy about what it allows to be passed via FTP, then you'll have to choose an HTTP URL rather than one of the FTP ones.

If you've already added URLs and got failure messages when you try to install modules, try adding an HTTP one as your first choice site. To do this, go to the CPAN command prompt:

perl -MCPAN -eshell

Once there enter:

o conf

to list your configuration settings. You'll see your urllist. Now enter:

o conf urllist unshift HTTP-URL

This sticks the URL at the top of the list. Depending on the age of your CPAN module and the way it's been set up, you may now have to do:

o conf commit

to make the change permanent, otherwise it will only last for the duration of the interactive session.

Now try installing the module again using:

install Collection::Module

To quit out of the CPAN shell enter:

quit

And if installing using the CPAN installer still fails, you can always just download the package from CPAN and install it manually using the instructions I described recently.

However, if you're somewhere where there's no access to the internet - or the server is locked down to prevent anything being installed from a remote source - you're going to have to use a copy of CPAN that you've downloaded previously onto a DVD or a thumb drive. For more on this have a listen to the Perlcast episode Making My Own CPAN by Brian D Foy.

Leave a comment



Installing Perl modules – a reminder

October 3rd, 2007

The simplest way to install a Perl module is to use the Perl CPAN module. Of course you have to install this first. Once you have you can do perl -MCPAN -e 'install Collection::Module'

For example: perl -MCPAN -e 'install Magic::Invisibility'

If the module isn't available for installation this way, but a package is available for download at the CPAN Web site, or elsewhere, you can usually install it the manual way. For example, following my recent post about a login banner, I recently installed the Text::Banner module on a Linux server as follows:

wget http://search.cpan.org/CPAN/authors/id/L/LO/LORY/Text-Banner-1.00.tar.gz
gunzip Text*gz
tar xvf Text*tar
cd Banner
cat README

... [I read the README file at this point]

perl Makefile.PL
make
make test
make install
perl -MText::Banner -e 1

[no error message means it was installed successfully]

For more detail (including details of how to install into a non-standard location) see:

http://search.cpan.org/~jhi/perl-5.8.0/pod/perlmodinstall.pod

 


UPDATE (13/10/2007):

I always used to use the  perl -MCollection::Module -e 1 way of checking whether a module was installed. Another, in some ways better, way to check is to print the version number of a module. To do this enter the command:

perl -e "use Collection::Module; print $Collection::Module::VERSION";

For example:

perl -e "use SOAP::Lite; print $SOAP::Lite::VERSION";

If the module is installed, this either prints the version number or, if the module hasn't been built to report its version, a blank line.

If the module is not installed, you get a "Can't locate ..." error message.

Leave a comment



Check whether a Perl module is installed

August 16th, 2005

To check whether a Perl module is installed, use the command: perl -M<module> For example: perl -MCGI or: perl -M<module>::<module>::<module> For example: perl -MIO::Socket::UNIX If perl returns a scree of stuff beginning "Can't locate...", then the module isn't installed. If all you get is a new line, the module is installed - in which case press Ctrl+D to get back to the command prompt.

Potentially similar posts

Leave a comment



^ back to top ^

Page 1 of 212»