Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: Cancel/no-op a require

by sedusedan (Monk)
on Dec 07, 2012 at 13:44 UTC ( #1007739=note: print w/replies, xml ) Need Help??

in reply to Re: Cancel/no-op a require
in thread Cancel/no-op a require

Ah, so this particular sentence in perlfunc is the one that gets me confused: The subroutine should return either nothing or else a list of up to three values in the following order.

So instead of returning (undef, sub{...}) I should've just returned (sub {...}).

Thanks, Toby.

Replies are listed 'Best First'.
Re^3: Cancel/no-op a require
by tobyink (Abbot) on Dec 07, 2012 at 14:07 UTC

    Also the innards of the function are kinda odd. You need to assign lines to $_ rather than returning them. I can understand why they did it that way, but it's pretty strange.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: Cancel/no-op a require
by Pickwick (Beadle) on Dec 30, 2014 at 15:14 UTC
    So instead of returning (undef, sub{...}) I should've just returned (sub {...}).

    The documentation is correct, returning your way might work, but is wrong according to the documentation. Instead I guess the important part you made wrong before is not returning a reference to a scalar for source code prefix as element 1. This is what I've been struggling with, which resulted in strange behaviour, like the returned filehandle in my returned list got read once, but not for a second file and such. Instead of returning a reference to a scalar, I tried to return undef, \'' or plain '', which all failed. When I started to really use a scalar, the return worked as documented: The file in my case got read using the handle and I could warn each line read in the given sub. It's important as well to really end the reading with 0 in the sub, else Perl will try to read data in some endless loop.

    my $prefix = ''; # undef is valid as well return (\$prefix, IO::File->new($path, '<:bytes'), sub { $_ =~ /^1;\s*$/ ? 0 : 1 }, undef);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007739]
[thao4]: I don't see where I can post my question. I am french so not good in English
[erix]: go to the bottom of this page: Seekers of Perl Wisdom
[karlgoethebier]: thao4: Si tout le reste échoue demande en francais ;-) There are some French speaking monks in the monastry...
[erix]: weird: a dev version perl (perl-5.27.9) is released, but git doesn't contain a perl-5.27.10 yet
[karlgoethebier]: ...not me ;-)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-02-21 10:01 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (276 votes). Check out past polls.