By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

$/ Problem

P: 6
'Lo Everyone,
I'm creating a script that parses XML files (already been done, I know, but I want to try it myself), and I've run into a bit of trouble. I am using the variable $/ , but I can only set it to a value that's one character long.
Expand|Select|Wrap|Line Numbers
  1. $/ = "a"; //works
  2. $/ = "\n"; //works
  3. $/ = "4"; //works
  4. $/ = "kitten"; //DOESN'T WORK!!!!?!?!?!?!?!?!?!?
  5. $/ = "\n\n"; //DOESN'T WORK !!!!?!?!?!?!?!?!?!?
  6.  
Does anybody know why I'm unable to assign a value to $/ if it's more than one character long? I learned perl from http://www.perl.org/books/beginning-perl/ and that said that you could set it to a multi-character string just fine.... any ideas?
I'm running perl 5.8.6, if that matters...?

Thanks All,
Andrew

PS: I tried using single quotes, double quotes, variables, plenty of options, but I couldn't make it work. Maybe I'm missing something... I hope so!
Jan 19 '08 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
It should be no problem, you can assign $/ a multi-character string as it's value. Must be something else wrong in your code, which we can't see.
Jan 19 '08 #2

P: 6
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <studentlist>
  3. <class id="Geography">
  4. <student>
  5. <name>Lexi</name>
  6. <gender>F</gender>
  7. </student>
  8.  
  9. <student>
  10. <name>Nelle</name>
  11. <gender>F</gender>
  12. </student>
  13.  
  14. <student>
  15. <name>Josh</name>
  16. <gender>M</gender>
  17. </student>
  18.  
  19. <student>
  20. <name>Jason</name>
  21. <gender>M</gender>
  22. </student>
  23.  
  24. <student>
  25. <name>Ben</name>
  26. <gender>M</gender>
  27. </student>
  28.  
  29. <student>
  30. <name>Larry</name>
  31. <gender>M</gender>
  32. </student>
  33. </class>
  34.  
  35. <class id="English">
  36. <student>
  37. <name>Caleb</name>
  38. <gender>M</gender>
  39. </student>
  40.  
  41. <student>
  42. <name>Emily</name>
  43. <gender>F</gender>
  44. </student>
  45.  
  46. <student>
  47. <name>Adelle</name>
  48. <gender>F</gender>
  49. </student>
  50.  
  51. <student>
  52. <name>Mike</name>
  53. <gender>M</gender>
  54. </student>
  55.  
  56. </class>
  57. </studentlist>
  58.  
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. #Using hashes
  3. use warnings;
  4. use strict;
  5.  
  6. open(XMLFILE, "students.xml");
  7.  
  8. $/ = "</student>";
  9.  
  10. for (<XMLFILE>){
  11. print $_;
  12. print "ALALALALALALALA\n";
  13. }
  14. close(XMLFILE);
  15.  
That's everything. People on other forums were unable to find any bugs... one person even said that it worked perfectly fine on HIS computer! Any ideas why it wouldn't work on mine?
Jan 20 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Was the XML file made on a windows computer and you are processing it on a Unix/Linux computer? Your code works for me on my Windows test server.
Jan 20 '08 #4

P: 6
The file was made with a Mac (typed exactly as it appears into TextEdit) and it was run on a Mac (the BSD/Unix concoction, Darwin).
GAAAAAAAARGH! :)
It makes no sense!
Jan 20 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
What is the output you get when you run the code?
Jan 20 '08 #6

P: 6
imac:~/begperl J$ perl abc.plx
??<?xml version="1.0" encoding="UTF-8"?>
<studentlist>
<class id="Geography">
<student>
<name>Lexi</name>
<gender>F</gender>
</student>

<student>
<name>Nelle</name>
<gender>F</gender>
</student>

<student>
<name>Josh</name>
<gender>M</gender>
</student>

<student>
<name>Jason</name>
<gender>M</gender>
</student>

<student>
<name>Ben</name>
<gender>M</gender>
</student>

<student>
<name>Larry</name>
<gender>M</gender>
</student>
</class>

<class id="English">
<student>
<name>Caleb</name>
<gender>M</gender>
</student>

<student>
<name>Emily</name>
<gender>F</gender>
</student>

<student>
<name>Adelle</name>
<gender>F</gender>
</student>

<student>
<name>Mike</name>
<gender>M</gender>
</student>

</class>
</studentlist>ALALALALALALALA
imac:~/begperl J$
There it is, run from the terminal.
Might the problem have to do with the "??" in font of the first line? An encoding problem, perhaps? It was saved as UTF-8, just like it says...
Jan 21 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
Sorry, but I don't know why you get that output. Maybe ask on perlmonks.com and see if anyone there has a clue.
Jan 21 '08 #8

P: 6
Yeah..... this has happened to me once before. I was programming on a TI-83+ Calculator, and it always got stuck on one line, even though everyone was sure that it was right. Thanks for your help. I'll try out the folks at perlmonks, but after that..... maybe I should update my version of perl, see if it goes away. Random works, sometimes......
Thanks again.
Jan 21 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.