On Tuesday, January 20, 2004, at 11:34 PM, Robin Sheat wrote:
Hey there, I'm not a total beginner to Perl, but am far enough into it
to have a lot of questions, so I hope this is a suitable place for them
:)

Fire away!


sub getResponse {
    my $self = shift;
    my $incoming = <$self->{filehandle}>;
    ...
gave me:
syntax error at NeuralPerl.pm line 50, near "<$self->{"

Typically, anything more complicated than a bareword (e.g. <FOO>) or a single variable (e.g. <$fh>) inside the angle-brackets will be interpreted as a glob(), rather than the readline() you wanted.

    % perldoc perlop
    ...
    If what's between the angle brackets is neither a filehandle
    nor a simple scalar variable [etc.]

So even if you were using a plain hash instead of the hashref

my $incoming = <$hash{filehandle}>;

This code still wouldn't do the right thing.

But you have a syntax error as well, because the hash-dereference
uses ">" as part of the "arrow", and so perl sees a matching pair
of angle brackets ("<" and ">") in the wrong place:

    <$self->{filehandle}>
    ^......^

You've already found one solution (put the filehandle in a simple
scalar variable) but you could also call readline() explicitly:

my $incoming = readline $self->{filehandle};

Oh, another one that I have hit a few times; is there a nice way of
doing what this looks like it means:
sub mySub {
        $foo = shift;
        @bar = shift;
        %baz = shift;
        ...

Currently I use things like @{ $_[1] } and so on, but I'm wondering if
there is a way I can use shift.

You can use shift() inside the dereference if you add parens:


my @bar = @{ shift() };

Or you can assign the hash/array references to typeglobs.

    sub f {
      $foo = shift;    # argument is a plain scalar
      *bar = shift;    # sets @bar, if $_[1] is an array reference
      *baz = shift;    # sets %baz, if $_[2] is a hash reference
      ...
    }

You can also just work with the references directly:

      my $bar = shift;
      push @$bar, 42;   # or whatever

And I think this is almost always the thing to do.

--
Steve


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to