[comp.lang.perl] Possible problem with syscall

gustav@tharr.UUCP (Paul Moore) (11/27/90)

There appears to be a problem with syscall() in perl. When perl is working
out whether an argument is a string or an integer, it decides by working out
whether the argument looks like a number. But what about the *string* "1"?

The above is a bit vague, as I have no experience of syscall() under unix,
so I can't say whether this is a real problem. I encountered it while
porting perl to my machine (the Acorn Archimedes - if anyone is interested
in this port, get in touch!), where I made syscall() call the operating
system routines. These include a "print a string" call, and I noticed the
problem when I tried to print "1" - as mentioned above.

However, from what I can gather of unix syscall(), it allows you to call
any system call available - so what about syscall(creat,"1",0666) [or
however you would say it] to create a file "1"?

Gustav.

-- 
------------------------------------------------------------------------------
Really: Paul Moore, 10, Mulberry Rise, Firdale Park, Northwich, ENGLAND
E-Mail: pmoore@cix.compulink.co.uk
    or: gustav@tharr.uucp (...!ukc!tharr!gustav)

"I hadda documentation once, but I drink a big-a glass of warm-a salty water
 anna it went away like-a dat" (Duke Leonardi, Cerebus 'Church & State')
------------------------------------------------------------------------------
[ Posted from: tharr - *free* public access to Usenet in the UK 0234 261804  ] 

tchrist@convex.COM (Tom Christiansen) (11/27/90)

In article <1454@tharr.UUCP> gustav@tharr.UUCP (Paul Moore) writes:
>system routines. These include a "print a string" call, and I noticed the
>problem when I tried to print "1" - as mentioned above.
>
>However, from what I can gather of unix syscall(), it allows you to call
>any system call available - so what about syscall(creat,"1",0666) [or
>however you would say it] to create a file "1"?

This is one of those times that perl cares about strings versus numerics.
Sometimes you might have to add 0 or concat a null to coerce to the right
type, but in practice I find this seldom to be the case.

There's a big difference between
    syscall(&SYS_write,1,1,1)
and
    syscall(&SYS_write,1,"1",1)

The first sets $! to indicate a bad address (0x01) for that expected
(char*) buffer to write (on my system, where the high bit indicates user
space, not system space as on a Vax).  The second actually writes a "1" to
STDOUT.

I course, you would really have uses syswrite() in the above case.

For the record, 

    syscall(&SYS_creat,"1",0666)

does indeed creat a file named "1", as you would hope.

--tom

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/28/90)

In article <1454@tharr.UUCP> gustav@tharr.UUCP (Paul Moore) writes:
: There appears to be a problem with syscall() in perl. When perl is working
: out whether an argument is a string or an integer, it decides by working out
: whether the argument looks like a number. But what about the *string* "1"?

No, it doesn't decide by whether it looks like a number, exactly.  It decides
by whether the value has ever been evaluated in a numeric context.  The
literal string "1" has never been evaluated in a numeric context, so
should be treated as a string.  If you have a string value of "1" in a
variable, but have evaluated it as a number (such as by comparing it using ==),
then it will be treated as a number.

Larry

worley@compass.uucp (Dale Worley) (11/29/90)

   From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall)

   No, it doesn't decide by whether it looks like a number, exactly.
   It decides by whether the value has ever been evaluated in a
   numeric context.

IMHO, this is a really weird rule.  I prefer a more Snobol-like rule
where a value knows whether it is a string or a number, and it retains
that identity.  (You can still use the "numeric value slot" of the
value to cache the result of convert-to-numeric.)

BTW, can I give "1" a numeric value by executing:

	"1" == 0

or am I missing something?

Dale Worley		Compass, Inc.			worley@compass.com
--
You think the government has trouble fighting a Drug War - wait until
they start the War on Cholesterol.  -- David Lively

markb@agora.uucp (Mark Biggar) (12/02/90)

In article <10543@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
>In article <1454@tharr.UUCP> gustav@tharr.UUCP (Paul Moore) writes:
>: There appears to be a problem with syscall() in perl. When perl is working
>: out whether an argument is a string or an integer, it decides by working out
>: whether the argument looks like a number. But what about the *string* "1"?
>
>No, it doesn't decide by whether it looks like a number, exactly.  It decides
>by whether the value has ever been evaluated in a numeric context.  The
>literal string "1" has never been evaluated in a numeric context, so
>should be treated as a string.  If you have a string value of "1" in a
>variable, but have evaluated it as a number (such as by comparing it using ==),
>then it will be treated as a number.

Note: you can force the variable $x to be a string for syscall by passing
$x.'' as the argument, $x+0 can be used to force it to be a number.

--
Perl's maternal uncle
Mark Biggar

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (12/28/90)

In article <1990Nov28.165453.28250@uvaarpa.Virginia.EDU> worley@compass.uucp writes:
: 
:    From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall)
: 
:    No, it doesn't decide by whether it looks like a number, exactly.
:    It decides by whether the value has ever been evaluated in a
:    numeric context.
: 
: IMHO, this is a really weird rule.  I prefer a more Snobol-like rule
: where a value knows whether it is a string or a number, and it retains
: that identity.  (You can still use the "numeric value slot" of the
: value to cache the result of convert-to-numeric.)

That's sort of what it does, unless I'm misunderstanding you.  Sorry
I didn't explain clearly.

: BTW, can I give "1" a numeric value by executing:
: 
: 	"1" == 0
: 
: or am I missing something?

Yes, that gives it a numeric value along with the string value.  Doing

	1 eq ''

forces the numeric value to also have a string value.

	1 .= ''

would force it to have ONLY a string value, and

	"1" += 0

forces it to have ONLY a numeric value.  (To avoid confusion, innocent
bystanders should note that we're not just talking about literal "1" and 1
here, but the state of a variable containing a string or numeric value.
You can't, in fact, assign to a literal in Perl.)

Larry