Why is $^O assignable?

Why is $^O assignable?

am 23.08.2007 12:38:33 von Andreas Puerzer

Greetings!

Recently I've stumbled over code where someone assigned to $^0. This
seemed rather nonsensical to me, so I tried:

D:\Temp>perl -MData::Dumper -we "use File::Spec; print Dumper(\%INC);"
$VAR1 = {
'warnings/register.pm' => 'C:/Perl/lib/warnings/register.pm',
'bytes.pm' => 'C:/Perl/lib/bytes.pm',
'XSLoader.pm' => 'C:/Perl/lib/XSLoader.pm',
'Carp.pm' => 'C:/Perl/lib/Carp.pm',
'C:/Perl/site/lib/sitecustomize.pl' =>
'C:/Perl/site/lib/sitecustomize.pl',
'File/Spec/Unix.pm' => 'C:/Perl/lib/File/Spec/Unix.pm',
'vars.pm' => 'C:/Perl/lib/vars.pm',
'strict.pm' => 'C:/Perl/lib/strict.pm',
'Exporter.pm' => 'C:/Perl/lib/Exporter.pm',
'warnings.pm' => 'C:/Perl/lib/warnings.pm',
'File/Spec.pm' => 'C:/Perl/lib/File/Spec.pm',
'overload.pm' => 'C:/Perl/lib/overload.pm',
'File/Spec/Win32.pm' => 'C:/Perl/lib/File/Spec/Win32.pm',
'Data/Dumper.pm' => 'C:/Perl/lib/Data/Dumper.pm'
};

So far, so good, everything as expected. File::Spec::Win32 shows up
because, as you may have guessed from my use of Doublequotes, I'm on
MSWin32.
But then I tried:

D:\Temp>perl -MData::Dumper -we "BEGIN { $^O = 'epoc'}; use File::Spec;
print Dumper(\%INC)"
$VAR1 = {
'File/Spec/Epoc.pm' => 'C:/Perl/lib/File/Spec/Epoc.pm',
'warnings/register.pm' => 'C:/Perl/lib/warnings/register.pm',
'bytes.pm' => 'C:/Perl/lib/bytes.pm',
'XSLoader.pm' => 'C:/Perl/lib/XSLoader.pm',
'Carp.pm' => 'C:/Perl/lib/Carp.pm',
'C:/Perl/site/lib/sitecustomize.pl' =>
'C:/Perl/site/lib/sitecustomize.pl',
'File/Spec/Unix.pm' => 'C:/Perl/lib/File/Spec/Unix.pm',
'vars.pm' => 'C:/Perl/lib/vars.pm',
'strict.pm' => 'C:/Perl/lib/strict.pm',
'Exporter.pm' => 'C:/Perl/lib/Exporter.pm',
'warnings.pm' => 'C:/Perl/lib/warnings.pm',
'File/Spec.pm' => 'C:/Perl/lib/File/Spec.pm',
'overload.pm' => 'C:/Perl/lib/overload.pm',
'Data/Dumper.pm' => 'C:/Perl/lib/Data/Dumper.pm'
};

No more File::Spec::Win32, but File::Spec::Epoc!
According to `perldoc perlvar`:
$OSNAME
$^O The name of the operating system under which this copy of Perl
was built, as determined during the configuration process.

So, I'm a little lost why it's even possible to (ab-)use $^O this way,
and I fail to see the possible benefits of doing so. OTOH, I'd have
expected to see something along the lines of:
Modification of a read-only value attempted

So, is this a bug or a feature? ;->
Could someone please shed some light on this?

Thank you,
Andreas Puerzer


--
Have Fun,
and if you can't have fun,
have someone else's fun.
The Beautiful South

Re: Why is $^O assignable?

am 23.08.2007 14:34:16 von Sisyphus

"Andreas Pürzer" wrote in message
news:5j564nF3riro0U1@mid.individual.net...
..
..
> So, I'm a little lost why it's even possible to (ab-)use $^O this way,

Damn good question ... I don't know the answer (and I look forward to seeing
responses from folk who *do* know).

> and I fail to see the possible benefits of doing so.

Me too. (Though there are benefits in being able to modify other %Config
values - see, for example, ExtUtils::FakeConfig.)

Mind you, it's interesting to note that:
1) $Config::Config{osname} is readonly;
2) $Config::Config{osname} can be modifed.

--------------------------------
C:\>perl -MConfig -we "$Config::Config{osname} = 'crap'"
%Config::Config is read-only

C:\>perl -MConfig -we "$obj=tied %Config::Config;$obj->{osname} =
'crap';print $Config::Config{osname}"
crap
--------------------------------

Cheers,
Rob

Re: Why is $^O assignable?

am 23.08.2007 16:26:48 von Ben Morrow

Quoth "Sisyphus" :
>
> "Andreas Pürzer" wrote in message
> news:5j564nF3riro0U1@mid.individual.net...
> .
> .
> > So, I'm a little lost why it's even possible to (ab-)use $^O this way,
>
> Damn good question ... I don't know the answer (and I look forward to seeing
> responses from folk who *do* know).

I can't answer, but there is special code in mg.c to make it possible,
and it has been there since at least 5.003.

> > and I fail to see the possible benefits of doing so.
>
> Me too.

I guess perhaps for testing? Presumably the File::Spec test suite, for
instance, contains some tests along the lines of

{
local $^O = 'MSWin32';
require File::Spec;
File::Spec->import;
# check that File::Spec::Win32 got loaded
}

Ben

--
I touch the fire and it freezes me, [ben@morrow.me.uk]
I look into it and it's black.
Why can't I feel? My skin should crack and peel---
I want the fire back... Buffy, 'Once More With Feeling'
--
2 + 2 = 5 for sufficiently large values of 2
[ben@morrow.me.uk]

Re: Why is $^O assignable?

am 23.08.2007 17:26:25 von Sisyphus

"Ben Morrow" wrote in message
news:82i0q4-v24.ln1@osiris.mauzo.dyndns.org...
..
..
>> > and I fail to see the possible benefits of doing so.
>>
>> Me too.
>
> I guess perhaps for testing? Presumably the File::Spec test suite, for
> instance, contains some tests along the lines of
>
> {
> local $^O = 'MSWin32';
> require File::Spec;
> File::Spec->import;
> # check that File::Spec::Win32 got loaded
> }

Seems a reasonable suggestion ... but, then again, I wonder if there's any
point in checking that File::Spec::Win32 gets loaded when the operating
system is not Win32. It would be a bit if a waste if you're running Solaris
and the tests fail simply because File::Spec::Win32 failed to load.

In other words why not simply:

require File::Spec;
File::Spec->import;
# check that File::Spec::$Config::Config{osname} got loaded.

(I actually did take a look at t/spec.t ... but couldn't summons the energy
to parse it :-)

Cheers,
Rob

Re: Why is $^O assignable?

am 23.08.2007 18:07:42 von xhoster

Andreas_Pürzer wrote:
> Greetings!
>
> Recently I've stumbled over code where someone assigned to $^0. This
> seemed rather nonsensical to me, so I tried:
>
> D:\Temp>perl -MData::Dumper -we "use File::Spec; print Dumper(\%INC);"
> $VAR1 = {
> 'warnings/register.pm' => 'C:/Perl/lib/warnings/register.pm',
> 'bytes.pm' => 'C:/Perl/lib/bytes.pm',
> 'XSLoader.pm' => 'C:/Perl/lib/XSLoader.pm',
> 'Carp.pm' => 'C:/Perl/lib/Carp.pm',
> 'C:/Perl/site/lib/sitecustomize.pl' =>
> 'C:/Perl/site/lib/sitecustomize.pl',
> 'File/Spec/Unix.pm' => 'C:/Perl/lib/File/Spec/Unix.pm',
> 'vars.pm' => 'C:/Perl/lib/vars.pm',
> 'strict.pm' => 'C:/Perl/lib/strict.pm',
> 'Exporter.pm' => 'C:/Perl/lib/Exporter.pm',
> 'warnings.pm' => 'C:/Perl/lib/warnings.pm',
> 'File/Spec.pm' => 'C:/Perl/lib/File/Spec.pm',
> 'overload.pm' => 'C:/Perl/lib/overload.pm',
> 'File/Spec/Win32.pm' => 'C:/Perl/lib/File/Spec/Win32.pm',
> 'Data/Dumper.pm' => 'C:/Perl/lib/Data/Dumper.pm'
> };
>
> So far, so good, everything as expected. File::Spec::Win32 shows up
> because, as you may have guessed from my use of Doublequotes, I'm on
> MSWin32.
> But then I tried:
>
> D:\Temp>perl -MData::Dumper -we "BEGIN { $^O = 'epoc'}; use File::Spec;
> print Dumper(\%INC)"
> $VAR1 = {
> 'File/Spec/Epoc.pm' => 'C:/Perl/lib/File/Spec/Epoc.pm',
> 'warnings/register.pm' => 'C:/Perl/lib/warnings/register.pm',
> 'bytes.pm' => 'C:/Perl/lib/bytes.pm',
> 'XSLoader.pm' => 'C:/Perl/lib/XSLoader.pm',
> 'Carp.pm' => 'C:/Perl/lib/Carp.pm',
> 'C:/Perl/site/lib/sitecustomize.pl' =>
> 'C:/Perl/site/lib/sitecustomize.pl',
> 'File/Spec/Unix.pm' => 'C:/Perl/lib/File/Spec/Unix.pm',
> 'vars.pm' => 'C:/Perl/lib/vars.pm',
> 'strict.pm' => 'C:/Perl/lib/strict.pm',
> 'Exporter.pm' => 'C:/Perl/lib/Exporter.pm',
> 'warnings.pm' => 'C:/Perl/lib/warnings.pm',
> 'File/Spec.pm' => 'C:/Perl/lib/File/Spec.pm',
> 'overload.pm' => 'C:/Perl/lib/overload.pm',
> 'Data/Dumper.pm' => 'C:/Perl/lib/Data/Dumper.pm'
> };
>
> No more File::Spec::Win32, but File::Spec::Epoc!
> According to `perldoc perlvar`:
> $OSNAME
> $^O The name of the operating system under which this copy of Perl
> was built, as determined during the configuration process.
>
> So, I'm a little lost why it's even possible to (ab-)use $^O this way,
> and I fail to see the possible benefits of doing so.

I can see doing that for various testing, debugging, and development
purposes.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

Re: Why is $^O assignable?

am 24.08.2007 16:14:08 von Andreas Puerzer

xhoster@gmail.com schrieb:
> Andreas_Pürzer wrote:
>
>>Greetings!
>>
>>Recently I've stumbled over code where someone assigned to $^0

>>
>>So, I'm a little lost why it's even possible to (ab-)use $^O this way,
>>and I fail to see the possible benefits of doing so.
>
>
> I can see doing that for various testing, debugging, and development
> purposes.
>
>
> Xho
>

Yes, testing, I had thought of that too, with the following scenario:

Developing a Module that is intended to behave differently on differing
platforms. Say I'm on Win32 and want to test the part of the Module
designed for MacOs, what good would it be to trick the Module into
believing being on MacOs, if, in fact it's still on Win32?
I mean, none of the MacOs code could possibly work, right?
At least not the code depending on the filesystem.
That's why I discarded the testing-answer.

But then again, for example File::Spec->devnull() could very well be
tested with $^O-magic.

So, your answer seems to be absolutely perfect.

I guess I was struck by perlvar talking about $^O in the past, so I
considered it to be a constant, the $OSNAME perl was built on.
Somehow I was under the impression there were 2 $^O-Variables, something
like $BUILD_OSNAME and $RUN_OSNAME. My bad...

Thanks for clearing things up,
Andreas Pürzer

--
Have Fun,
and if you can't have fun,
have someone else's fun.
The Beautiful South

Re: Why is $^O assignable?

am 24.08.2007 16:25:03 von Andreas Puerzer

Ben Morrow schrieb:

> Quoth "Sisyphus" :
>
>>"Andreas Pürzer" wrote in message
>>news:5j564nF3riro0U1@mid.individual.net...
>>.
>>.
>>
>>>So, I'm a little lost why it's even possible to (ab-)use $^O this way,
>>
>>Damn good question ... I don't know the answer (and I look forward to seeing
>>responses from folk who *do* know).
>
>
> I can't answer, but there is special code in mg.c to make it possible,
> and it has been there since at least 5.003.
>

Magical Case '\017', I see...
So $^0 can be used as both the historical value while building, as well
as the currently running OS, very nice.
But why am I surprised, it's Perl! ;->

>
>>>and I fail to see the possible benefits of doing so.
>>
>>Me too.
>
>
> I guess perhaps for testing? Presumably the File::Spec test suite, for
> instance, contains some tests along the lines of
>
> {
> local $^O = 'MSWin32';
> require File::Spec;
> File::Spec->import;
> # check that File::Spec::Win32 got loaded
> }
>
> Ben
>

This seems to be 'The Answer'.

Thanks to both Sisyphus and Ben Morrow for helping me understand,
Andreas Pürzer

--
Have Fun,
and if you can't have fun,
have someone else's fun.
The Beautiful South