Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

XML::Twig Question (sorta)

by zericm (Novice)
on Mar 28, 2009 at 17:59 UTC ( [id://753878]=perlquestion: print w/replies, xml ) Need Help??

zericm has asked for the wisdom of the Perl Monks concerning the following question:

All,

I've got this pretty simple chunk of code here:

@projectEvents = ($dcrTwigCopy->descendants('tab[@tabType="events" +]')); if (@projectEvents) { print "\n\nWe have an array!\n"; populateEventManifest(@projectEvents, $eventManFh); } sub populateEventManifest { my( @events, $eventManFh ) = @_; foreach my $event (@events) { $event->print($eventManFh); } }

When I do that final print statement to a file handle, I get:

Use of uninitialized value in print at /System/Library/Perl/5.8.8/darwin-thread-multi-2level/IO/Handle.pm line 401.

But when I drop the filehandle, it prints fine to STDOUT. Not looking for a specific answer here (I'd rather learn to fish), but some suggestions as to where I should cast my net.

Thanks,
Eric

Replies are listed 'Best First'.
Re: XML::Twig Question (sorta)
by Anno (Deacon) on Mar 28, 2009 at 18:23 UTC
    The line my( @events, $eventManFh ) = @_; will never assign anything to
    $eventManFh.

    my $eventManFh = pop; my @events = @_; # ...

    Anno

Re: XML::Twig Question (sorta)
by ikegami (Patriarch) on Mar 28, 2009 at 19:12 UTC

    Anno is quite right. Here's a quick explanation.

    When you call a subroutine, the arguments are flattened into a list.

    sub foo { print("There are ", 0+@_, " args\n"); print("They are @_\n"); } my @a = qw( a b c ); my $n = 123; foo(@a, $n);
    There are 4 args They are a b c 123

    For this reason, an array reference is usually passed instead of the contents of the array.

    foo(\@a, $n);
    There are 2 args They are ARRAY(0x1829aec) 123

    In your case, it would be

    populateEventManifest(\@projectEvents, $eventManFh); # <-- extra \ sub populateEventManifest { my( $events, $eventManFh ) = @_; # <-- scalar foreach my $event (@$events) { # <-- extra $ $event->print($eventManFh); } }

    Anno provided an alternative solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://753878]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found