Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Filehandle of the current sourcecode, pointing after last code line?

by shmem (Chancellor)
on Oct 13, 2014 at 09:19 UTC ( [id://1103598]=note: print w/replies, xml ) Need Help??


in reply to Filehandle of the current sourcecode, pointing after last code line?

Just fully qualify your file handles.

# file Foo.pm package Foo; 1; __DATA__ Foo foo foo foo
# file Bar.pm package Bar; 1; __DATA__ print Bar. I told you so.

Last, the script...

#!/usr/bin/perl use Foo; use Bar; print while <DATA>; print "Foo:\n"; print while <Foo::DATA>; print "Bar:\n"; print while <Bar::DATA>; __DATA__ so this works. __END__ so this works. Foo: Foo foo foo foo Bar: print Bar. I told you so.

If you can read from the different DATA filehandles, you've got them. ;)

Parting from there, you can get at the file name via fileno and /proc/$$/fd/ (on linux, that is):

#!/usr/bin/perl use Foo; use Bar; print while <DATA>; system "ls -l /proc/$$/fd"; print "Foo: ",fileno Foo::DATA, "\n"; print while <Foo::DATA>; print "Bar: ",fileno Bar::DATA, "\n"; print while <Bar::DATA>; __DATA__ so this works.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Filehandle of the current sourcecode, pointing after last code line?
by LanX (Saint) on Oct 13, 2014 at 09:45 UTC
    Did you read the "motivation" part? :)

    update
    I.e. I have no control about the inspected code and it has to be cross platform...

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Did you read the "motivation" part? :)

      Yes, I did. And I can't see any bug there regarding DATA. Maybe I don't get the point.

      package Quux; our $foo = "blorflydick"; print "in Quux\n"; print while <DATA>; print $foo,$/; 1; package blorflydick; print $foo,$/; __DATA__ blorf, blorf.
      #!/usr/bin/perl use Foo; use Bar; use Quux; print while <DATA>; print "Foo (",fileno Foo::DATA, ")\n"; print while <Foo::DATA>; print "Bar (",fileno Bar::DATA, ")\n"; print while <Bar::DATA>; print "Symbol table Quux::\n"; print "$_ => $Quux::{$_}\n" for sort keys %Quux; print "Symbol table blorflydick::\n"; print "$_ => $blorflydick::{$_}\n" for sort keys %blorflydick::; print "Quux (",fileno Quux::DATA, ")\n"; print while <Quux::DATA>; print "ok, trying blorflydick\n"; print "blorflydick (",fileno blorflydick::DATA, ")\n"; print while <blorflydick::DATA>; package Baz; __DATA__ so this works. __END__ in Quux blorflydick blorflydick Foo (4) Foo foo foo foo Bar (5) print Bar. I told you so. Symbol table Quux:: Symbol table blorflydick:: DATA => *blorflydick::DATA Quux () ok, trying blorflydick blorflydick (6) blorf, blorf.

      I think of the DATA filehandle as an alias to the fully qualified DATA filehandle with similar semantics as our, with the difference that it isn't file scoped like our variables are.

      So, the blorflydick::DATA filehandle points to Quux.pm which is not a bug IMHO.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        It's a "bug" regarding B::Deparse which tries to guess which DATA handle is pointing to the end of the current file.

        Like demonstrated one can have 2 such opened handles (in main and current package) and non points to that current file.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found