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;

In reply to Re^2: __FILE__ fails in Mojolicious app built with PAR::Packer by metaperl
in thread __FILE__ fails in app built with PAR::Packer by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.