f'ups rearranged

> > [EMAIL PROTECTED] wadet]$ perl
> > sub box {
> >   return('<img src="p.gif" height="'.$_[0].'" width="'.$_[1].'">');
> > }
> > print <<eot;
> > <table>
> >   <tr>
> >     <td>${\box(5,10)}</td>
> >     <td>${\box(7,10)}</td>
> >   </tr>
> > </table>
> > eot
> > Ctrl-D
> > <table>
> >   <tr>
> >     <td><img src="p.gif" height="5" width="10"></td>
> >     <td><img src="p.gif" height="7" width="10"></td>
> >   </tr>
> > </table>
> >
> > see:
> >
> > [EMAIL PROTECTED] wadet]$ perldoc perlref

"Peter Kappus" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
...
> Aha!  This is exactly the kind of solution I was looking for.  I guess
what
> I'm doing here is passing a reference to a subroutine call?  \box(5,10)
and
> then turning it into a scalar?  ${}

Almost. In most contexts in perl, a { } construct is a BLOCK, and all code
in between the braces will be evaluated. So for our example, we call box
then return a reference to box()'s return value.

if a reference logically follows a $ in an interpolated string, the the
reference will be dereferenced.

Hence the desired results.

If (for some reason) we were passing a reference to the subroutine, it (
may ) look like this:

[EMAIL PROTECTED] wadet]$ perl
sub box {
  return('<img src="p.gif" height="'.$_[0][0].'" width="'.$_[0][1].'">');
}
print <<eot;
<table>
  <tr>
    <td>${\box( [5,10] )}</td>
    <td>${\box( [7,10] )}</td>
  </tr>
</table>
eot
Ctrl-D
<table>
  <tr>
    <td><img src="p.gif" height="5" width="10"></td>
    <td><img src="p.gif" height="7" width="10"></td>
  </tr>
</table>

and if you want to go OO:

package My::Img;

sub new {
  my($class, %params) = @_;
  return( bless( { %params }, $class ) );
}

sub box {
  my($self) = shift();
  return('<img src="p.gif" height="'.$self->{height}.'"
width="'.$self->{width}.'">');
}

package main;

print <<eot;
<table>
  <tr>
    <td>${\ My::Img->new(height =>5, width => 10)->box() }</td>
    <td>${\ My::Img->new(height =>10, width => 5)->box() }</td>
  </tr>
</table>
eot
Ctrl-D
<table>
  <tr>
    <td><img src="p.gif" height="5" width="10"></td>
    <td><img src="p.gif" height="10" width="5"></td>
  </tr>
</table>

but thats just silly ;0)

dont forget:

> > see:
> >
> > [EMAIL PROTECTED] wadet]$ perldoc perlref

and for a real good time, check out

[EMAIL PROTECTED] wadet]$ perldoc perllol

> Quite ingenious.  While I'm sure I'll regret it later (and curse your good
> name as I'm wishing I'd taken the time to learn Mason or HTML::Template)
> this seems like a great quick fix for the time being.  And it never hurts
to
> learn a few new tricks!

Unfortunately theres nothing there I came up with on my own, and yes, new
tricks are always good for the 'ol grab bag =0).

HTH,

Todd W.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to