The question is not how to tell in a oneliner. If you're writing the code in a one-liner, you know you are. But how does a module, included by -MMy::Module::Name
know that it all started from a oneliner.
This is mine. It's non-portable though and relies on UNIX standard commands (although, it can be made portable more or less.)
my $process_info = `ps $$ | tail -1`;
my $is_oneliner
= $process_info =~ m/perl.*?\s+-[^\P{IsLower}e]*e[^\P{IsLower}e]*\s+/m
;
And if you have a snazzier regex, feel free to improve upon mine.
A couple of people have asked why I would want to do this. brian correctly guessed that I wanted to change export behavior based on whether it's a script, which we can assume has had some amount of design, or whether it's a oneliner where the user is trying to do as much as possible in a single command line.
This sounds bad, because there's this credo that exporters should respect other packages--sometimes known as "@EXPORT
is EVIL!" But it seems to me that it's a foolish consistency when applied to oneliners. After all perl itself goes out of it's way to violate the structure of its language and give you easy loops if you ask for them on the command line, I simply want to extend that idea for my operational/business domain. I even want to apply source filters (gasp!) if it helps.
But this question also suggests that I might want to be a good citizen of Perl as well, because I only to break the community guidelines in certain cases. It is quite awesome to be able to create major business-level actions just by changing the command line in a batch scheduler rather than writing a whole new module. The test cycle is much compressed.