Maximum string length in perl

M

Murugesh

hi all,

Anyidea on the maximum length a string in perl can contain.I
cannot read the whole content of a file into a string,where it trims off
after a particular limit.Is there any restriction.
Is there any way to read whole file into a single string?

Thanks,
Appu
 
A

A. Sinan Unur

Anyidea on the maximum length a string in perl can contain.

There is no pre-defined limit. On the other hand, perl is subject to the
limits imposed by the OS etc.
I cannot read the whole content of a file into a string,

How about posting a short, complete example that exhibits the problem? How
about telling us the size of the file you are trying to read?
where it trims off after a particular limit.

What 'particular limit'?
Is there any way to read whole file into a single string?

Yes.

Sinan
 
F

Fabian Pilkowski

* Murugesh said:
Anyidea on the maximum length a string in perl can contain.I
cannot read the whole content of a file into a string,where it trims off
after a particular limit.Is there any restriction.

No, there isn't one.
Is there any way to read whole file into a single string?

I prefer to use

my $str = do{ undef $/; <FH> };

but on windowish systems this could be

my $str = do{ undef $/; binmode FH; <FH> };

to read files with binary content correctly. From `perldoc -f binmode`:

Another consequence of using binmode() (on some systems) is that
special end-of-file markers will be seen as part of the data stream.
For systems from the Microsoft family this means that if your binary
data contains \cZ, the I/O subsystem will regard it as the end of
the file, unless you use binmode().

regards,
fabian
 
M

Murugesh

Fabian,

Thanks ! that works!
My content is not a binary file.In Windows, I tried ,
my $str = do{ undef $/; <FH> };
My question is,

why the follwing code doesnt work,
$/="";
my $str=<FH>;

How does the above code is different from undef $/;
 
J

John W. Krahn

Murugesh said:
Thanks ! that works!
My content is not a binary file.In Windows, I tried ,
my $str = do{ undef $/; <FH> };

You shouldn't undef() a global variable like that, it will affect your whole
program. You should use local() instead:

My question is,

why the follwing code doesnt work,
$/="";
my $str=<FH>;

How does the above code is different from undef $/;

Because assigning a zero length string to the input record separator variable
sets it to paragraph mode.

perldoc perlvar



John
 
F

Fabian Pilkowski

* John W. Krahn said:
You shouldn't undef() a global variable like that, it will affect your whole
program. You should use local() instead:

my $str = do{ local $/; <FH> };

Err... this is the second time I'm doing this mistake. Each time it was
in a newsgroup so that everyone could see my inattention ;-( Of course,
I want to localize that var ...

But also, each time one corrects my intention a few minutes later ;-)

thanks,
fabian
 
F

Fabian Pilkowski

* Murugesh said:
Thanks ! that works!
My content is not a binary file.In Windows, I tried ,
my $str = do{ undef $/; <FH> };
My question is,

why the follwing code doesnt work,
$/="";
my $str=<FH>;

How does the above code is different from undef $/;

Please take notice what John has posted. Use local() instead of undef().
But in

my $str = do{ local $/; <FH> };

the value of $/ is *undef* since I don't assign any value. In your case
$/ contains an empty string, which isn't the same. All of these values
are described in `perldoc perlvar`. Have a look at it.

Btw, please localize your $/-assignment into an own block, otherwise
this will affect your whole program.

regards,
fabian
 
T

Tad McClellan

Please do not top-post. Learn how to compose a proper followup.

Thanks ! that works!
my $str = do{ undef $/; <FH> };
My question is,

why the follwing code doesnt work,
$/="";
my $str=<FH>;

How does the above code is different from undef $/;


If you are wondering about the values of the $/ variable, then
you should look up the $/ variable in the docs that came with perl:

perldoc perlvar

treating empty lines as a terminator if set to
the null string.
...
You may set it ... to undef to read through the end of file.
...



So, setting it to the empty string (para mode) reads records
that are separated by blank lines (and your code above only
does one read, it never looks at the rest of the file).

Setting it to undef (slurp mode) reads the *entire file*
in one input operation.



[snip TOFU]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top