The perlish way to write this? 
Author Message
 The perlish way to write this?

Hi,

Is this the best way to write this sub?  If not what would be an
improvement?

sub imageButton {

  $alt    = "" if !$alt;
  $border = "0" if !$border;
  $align  = "abscenter" if !$align;

  print "<a href=$href><img src=$image align=$align border=$border
alt=\'$alt\'></a>";

Quote:
}

Thanks!


Mon, 19 Jan 2004 05:51:08 GMT  
 The perlish way to write this?

  B> sub imageButton {

  B>   $alt    = "" if !$alt;

the ||= op is a common idiom for assigning defaults. it's only weakness
is if you had a 0 in there and wanted to keep it.

  B>   $border = "0" if !$border;
  B>   $align  = "abscenter" if !$align;

        $border ||= '0' ;
        $align ||= 'abscenter ;

  B>   print "<a href=$href><img src=$image align=$align border=$border
  B> alt=\'$alt\'></a>";

yech! learn about perl's great variant quoting operators. either qq or a
here doc would be much nicer there. you don't need to escape ' in a
normal double quoted string. also you need quotes around all the tag
values for legal html

for html i almost always use here docs:

        print <<HTML ;
<a href="$href"><img src="$image" align="$align" border="$border"
alt="$alt"></a>
HTML

uri

--

SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org



Mon, 19 Jan 2004 08:26:30 GMT  
 The perlish way to write this?

Quote:

> Hi,

> Is this the best way to write this sub?  If not what would be an
> improvement?

> sub imageButton {

>   $alt    = "" if !$alt;
>   $border = "0" if !$border;
>   $align  = "abscenter" if !$align;

>   print "<a href=$href><img src=$image align=$align border=$border
> alt=\'$alt\'></a>";
> }

sub imageButton {

   $alt    ||= '';
   $border ||= 0;
   $align  ||= 'abscenter';
   print <<EOH;
       <a href="$href">
           <img src="$image" align="$align" border="$border" alt="$alt">
       </a>
EOH

Quote:
}

The or-equal (||=) is useful for situations like this where you don't
care about the difference between an undefined and a false value.  This
is also better HTML than yours because you don't know what is going to
go inside the attributes and it may well be something that requires
quote marks.  

Depending on preference, you might also want to re-write the entire
thing using CGI.pm because that would (IIRC) take care of the undefs for
you and would be even more legible in terms of mapping variables to
attributes.  I believe it would also take care of the case that you
haven't covered where one of the attributes contained an embedded quote
mark that would mess up the attribute delimiters.

So *you're* the guy that prevents me from signing up for all those
online thingies.  They keep telling me that email address is already
registered.

--
Jeff



Mon, 19 Jan 2004 08:38:45 GMT  
 The perlish way to write this?

Quote:

> Hi,
> Is this the best way to write this sub?  If not what would be an
> improvement?
> sub imageButton {

>   $alt    = "" if !$alt;
>   $border = "0" if !$border;
>   $align  = "abscenter" if !$align;
>   print "<a href=$href><img src=$image align=$align border=$border
> alt=\'$alt\'></a>";
> }

You've already gotten several replies about using the ||= idiom.  My
suggestion is that when you've got a subroutine that takes several
optional parameters, you make them keyword parameters rather than
positional parameters.  Perl's magical autoconversion between lists and
hashes makes it extremely easy to implement keyword parameters:

sub imageButton {


# when you initialize an array from a list and there are duplicate keys,
# the last one takes precedence

  print "<a href=$href><img src=$image align=$opts{align}
border=$opts{border} alt='$opts{alt}'></a>";

Quote:
}

Now you could call it like:

imageButton('home','house.gif');  #uses default for all options
imageButton('home','house.gif',alt=>'Home');
imageButton('home','house.gif',alt=>'Home',border=>1);



Mon, 19 Jan 2004 11:34:47 GMT  
 The perlish way to write this?

Quote:


>   B> sub imageButton {

>   B>   $alt    = "" if !$alt;

> the ||= op is a common idiom for assigning defaults. it's only weakness
> is if you had a 0 in there and wanted to keep it.

>   B>   $border = "0" if !$border;
>   B>   $align  = "abscenter" if !$align;

>    $border ||= '0' ;
>    $align ||= 'abscenter ;

Just curious, Uri, as to why you prefer the above to the shift ||
syntax. (I added the mandatory stuff on my own volition) Is it a speed
issue or just personal preference?

sub imageButton {
    my $href  =shift || die "Missing mandatory parameter \$href\n";
    my $image =shift || die "Missing mandatory parameter \$image\n";
    my $alt   =shift || "";
    my $border=shift || "0";
    my $align =shift || "abscenter";

Yves

- Show quoted text -

Quote:

>   B>   print "<a href=$href><img src=$image align=$align border=$border
>   B> alt=\'$alt\'></a>";

> yech! learn about perl's great variant quoting operators. either qq or a
> here doc would be much nicer there. you don't need to escape ' in a
> normal double quoted string. also you need quotes around all the tag
> values for legal html

> for html i almost always use here docs:

>    print <<HTML ;
> <a href="$href"><img src="$image" align="$align" border="$border"
> alt="$alt"></a>
> HTML

> uri



Mon, 19 Jan 2004 22:30:03 GMT  
 The perlish way to write this?

Quote:

>         $border ||= '0' ;
>         $align ||= 'abscenter ;

Ahh, much nicer there....

Quote:

> for html i almost always use here docs:

>         print <<HTML ;
> <a href="$href"><img src="$image" align="$align" border="$border"
> alt="$alt"></a>
> HTML

Thanks Uri (And everyone else too!) for the nice tips.  One question on
the here doc though.  I agree that it is the best suited for this kind
of thing, but visually it is a pain in the a$$.  Maybe someone knows a
way around this particular annoyance with here docs?

Example:
sub A_Test_Sub {
   if ($someValue == 1) {
      if ($someOtherValue eq "yes") {
         $str = <<HTML;
          <a href=http://www.slashdot.com>Slashdot</a>
HTML
^^^^              
      }
   } elsif ($someValue == 2) {
      # do stuff
   } elsif ($someValue == 3) {
      # do other stuff
   }

Quote:
}

Notice the HTML end must have no leading whitespace... visually very
distracting (for me anyaway).  I know you can do $str =<<"    HTML", but
then in my rapidly changing development environment (read correcting my
mistakes :) everytime an indent changes I have to go and recount spaces
so my here doc works again.  Maybe some neat little regex works?

Thanks!
Bryan



Tue, 20 Jan 2004 01:20:59 GMT  
 The perlish way to write this?

[snipped]

Quote:
>Notice the HTML end must have no leading whitespace... visually very
>distracting (for me anyaway).  I know you can do $str =<<"    HTML", but
>then in my rapidly changing development environment (read correcting my
>mistakes :) everytime an indent changes I have to go and recount spaces
>so my here doc works again.  Maybe some neat little regex works?

Have a look at perlfaq 4: "Why don't my <<HERE documents don't work".
This might help you.

Tassilo

--
Shick's Law:
        There is no problem a good miracle can't solve.



Tue, 20 Jan 2004 01:37:18 GMT  
 The perlish way to write this?

Quote:

> Notice the HTML end must have no leading whitespace... visually very
> distracting (for me anyaway).  I know you can do $str =<<"    HTML", but
> then in my rapidly changing development environment (read correcting my
> mistakes :) everytime an indent changes I have to go and recount spaces
> so my here doc works again.  Maybe some neat little regex works?

You could always use the same obscure word to end your here docs and
then yes, a RE would be able to solve it:

        s/^\s+(oBsCuRe)\s*$/$1/;

Where oBsCuRe is whatever word you choose. :)  Maybe a vi macro. :)

--
  ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852

  / ) /--<  o // //      http://dbecoll.webjump.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/



Tue, 20 Jan 2004 03:49:24 GMT  
 The perlish way to write this?


  >> $border ||= '0' ;
  >> $align ||= 'abscenter ;

  YO> Just curious, Uri, as to why you prefer the above to the shift ||
  YO> syntax. (I added the mandatory stuff on my own volition) Is it a speed
  YO> issue or just personal preference?

  YO> sub imageButton {
  YO>     my $href  =shift || die "Missing mandatory parameter \$href\n";
  YO>     my $image =shift || die "Missing mandatory parameter \$image\n";
  YO>     my $alt   =shift || "";
  YO>     my $border=shift || "0";
  YO>     my $align =shift || "abscenter";

well, what if you were passing in key/value pairs in a hash? someone
else posted a common trick for that with a new hash being initialed with

style for sub args and not to shift for each one. also not all params
need a default so doing what you have is slightly overkillish. but your
style is ok. put some whitespace between the = and shift. :)

uri

--

SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org



Tue, 20 Jan 2004 04:45:17 GMT  
 The perlish way to write this?
[snip]

Quote:
> Thanks Uri (And everyone else too!) for the nice tips.  One question
> on the here doc though.  I agree that it is the best suited for this
> kind of thing, but visually it is a pain in the a$$.  Maybe someone
> knows a way around this particular annoyance with here docs?

> Example:
> sub A_Test_Sub {
>    if ($someValue == 1) {
>       if ($someOtherValue eq "yes") {
>          $str = <<HTML;
>           <a href=http://www.slashdot.com>Slashdot</a>
> HTML
> ^^^^
>       }
>    } elsif ($someValue == 2) {
>       # do stuff
>    } elsif ($someValue == 3) {
>       # do other stuff
>    }
> }

> Notice the HTML end must have no leading whitespace... visually very
> distracting (for me anyaway).  I know you can do $str =<<"    HTML",
> but then in my rapidly changing development environment (read
> correcting my mistakes :) everytime an indent changes I have to go and
> recount spaces so my here doc works again.  Maybe some neat little
> regex works?

Maybe in the future, we'll be allowed to use an RE instead of a literal
string, so you'll be able to do

  $str = <<qr/^\s*HTML$/;
    asdfasldkfjasd
  HTML

With whatever amount of whitespace you want in front of "HTML"

--
I need more taglines. This one is getting old.



Mon, 26 Jan 2004 05:40:58 GMT  
 The perlish way to write this?

  >> Notice the HTML end must have no leading whitespace... visually very
  >> distracting (for me anyaway).  I know you can do $str =<<"    HTML",
  >> but then in my rapidly changing development environment (read
  >> correcting my mistakes :) everytime an indent changes I have to go and
  >> recount spaces so my here doc works again.  Maybe some neat little
  >> regex works?

  BG> Maybe in the future, we'll be allowed to use an RE instead of a literal
  BG> string, so you'll be able to do

  BG>   $str = <<qr/^\s*HTML$/;
  BG>     asdfasldkfjasd
  BG>   HTML

  BG> With whatever amount of whitespace you want in front of "HTML"

perl6 will have more flexible here doc indenting with removal of common
white space prefixes (the amount before the end token). this feature has
been requested by many people. maybe it will be first supported in a
later version of 5.

uri

--

SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org



Mon, 26 Jan 2004 05:59:42 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. more than one statement in a post perlish condition

2. ANNOUNCE: Dumper - A package to recursively dump vars in Perlish

3. Match Parens-More Perlish?

4. Perlish linguistics (aka shibbolethim)

5. Is this "perlish"

6. A Simpler perlish way

7. More Perlish file size (bytes, KB, MB)

8. perlish idiom for substitute in many files, only when pattern found

9. obfuscated but useful magic() - for your perlish amusement

10. Class library to make C++ more Perlish?

11. PERLish equivalents for REXX functions wanted.

12. is this perlish?

 

 
Powered by phpBB® Forum Software