Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: __FILE__ fails in Mojolicious app built with PAR::Packer

by Anonymous Monk
on Sep 10, 2011 at 04:38 UTC ( #925195=note: print w/ replies, xml ) Need Help??


in reply to __FILE__ fails in app built with PAR::Packer

No, __FILE__ does not fail

__FILE__ is not guaranteed to be an absolute path (use lib '.'), or a real file (-e)

If __FILE__ is relative, and something does chdir, __FILE__ will point to the wrong thing

You could argue it is a bug to rely on __FILE__ as always being absolute

I would argue it is a bug to put relative paths into @INC/%INC

See chdir and relative paths in @INC

BEGIN { use File::Spec; for (@INC) { if ( !ref $_ && -d $_ && !File::Spec->file_name_is_absolute($_ +) ) { $_ = File::Spec->rel2abs($_); } } }


Comment on Re: __FILE__ fails in Mojolicious app built with PAR::Packer
Download Code
Re^2: __FILE__ fails in Mojolicious app built with PAR::Packer
by metaperl (Curate) on Sep 10, 2011 at 20:27 UTC
    If __FILE__ is relative, and something does chdir, __FILE__ will point to the wrong thing
    I noticed you said *if* __FILE__ is relative. When is it relative and when not? A simple program I wrote to trip up __FILE__ yields an absolute path for some reason:
    sub hello { chdir '..'; my $file = __FILE__; warn "My file name is $file. Here is my contents:"; open(my $fh, "<", $file); my @data = <$fh>; warn "@data"; } hello(); 1;
    Maybe there is something wrong with the chdir, I am doing to keep my Mojolicious::Lite app working under PAR::Packer... perhaps ... I will meditate on this a bit more.
    You could argue it is a bug to rely on __FILE__ as always being absolute
    Yes, I also think it's a less-than-appealing that __FILE__ is not always absolute, dont you? And when is __FILE__ computed? Runtime? compiletime? How is it done?
    I would argue it is a bug to put relative paths into @INC/%INC See chdir and relative paths in @INC
    Yes. Thank you for that data. That could save me a few hours of debugging at some point. However, the whole topic of loading files and relative paths on @INC is tangential to the issue with __FILE__, correct?

    I tried putting the @INC fixing code at the top of a script demonstrating the __FILE__ problem with PAR and still have the anomaly when using PAR. What chdir command might fix this? I will meditate on that right after this post.

    # works in plain perl, fails with this compilation line: # pp -P -r -v 99 -o packed.pl somefile.pl BEGIN { use File::Spec; for (@INC) { if ( !ref $_ && -d $_ && !File::Spec->file_name_is_absolute($_ +) ) { $_ = File::Spec->rel2abs($_); } } } sub hello { my $file = __FILE__; warn "My file name is $file. Here is my contents:"; open(my $fh, "<", $file); my @data = <$fh>; warn "@data"; } hello(); 1;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-09-21 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls