7 Replies - 335 Views - Last Post: 13 May 2014 - 12:35 PM Rate Topic: -----

#1 _WolfShade_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 12-May 14

Weird switch/case issue

Posted 13 May 2014 - 10:18 AM

Hello,

I'm getting a weird switch/case issue. I've always understood that the reason why switch/case statements are more efficient than if/else is because if/else statements will execute every condition but only use the matching code in the process, whereas a switch/case will utilize ONLY the code contained within the matching case, and ignore everything else.

I've got the following code that, when run in a Windows environment, is executing the *nix code that it _should_ be ignoring.

<cfoutput>#FindNoCase('windows',server.os.name)#</cfoutput><!--- Outputs '1' --->
<cfswitch expression="#FindNoCase('windows',server.os.name)#">
  <cfcase value="0">
    <cffile action="write" addnewline="no" file="#trim(ARGUMENTS.file)#"
        output="" fixnewline="yes" mode="700" /><!--- Executes this, giving an error --->
  </cfcase>
  <cfdefaultcase>
    <cffile action="write" addnewline="no" file="#trim(ARGUMENTS.file)#"
        output="" fixnewline="yes" attributes="readOnly" />
  </cfdefaultcase>
</cfswitch>


Attribute validation error for tag CFFILE.
It has an invalid attribute combination.

I'm stumped. Any ideas?

V/r,

Is This A Good Question/Topic? 0
  • +

Replies To: Weird switch/case issue

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1926
  • View blog
  • Posts: 3,471
  • Joined: 13-January 08

Re: Weird switch/case issue

Posted 13 May 2014 - 11:13 AM

Hey _Wolfshade_:

That does seem a little odd. It seems like your code would work (minus the bold HTML within the expression) but if it's not, please consider trying this:

<cfset variables.WinOS = FindNoCase('windows',server.os.name)>
<cfswitch expression="#variables.WinOS#">
	<cfcase value="0">
		<cffile action="write" addnewline="no" file="#trim(ARGUMENTS.file)#"
			output="" fixnewline="yes" mode="700" />
	</cfcase>
	<cfcase value="1">
		<cffile action="write" addnewline="no" file="#trim(ARGUMENTS.file)#"
			output="" fixnewline="yes" attributes="readOnly" />
	</cfcase>
	<cfdefaultcase>
		<cffile action="write" addnewline="no" file="#trim(ARGUMENTS.file)#"
			output="" fixnewline="yes" attributes="readOnly" />
	</cfdefaultcase>
</cfswitch>


I'd explicitly submit the value = 1 case rather than rely on it being the default case. I'd also set the value as a variable setting and make the switch evaluate it. Outside of that, this seems like it ought to work. Give this a try and see what it does.

Good luck!

This post has been edited by Craig328: 13 May 2014 - 11:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 _WolfShade_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 12-May 14

Re: Weird switch/case issue

Posted 13 May 2014 - 11:18 AM

Thank you, sir. Not sure how the bold tag got in there, but..

I'll give this a shot and see what happens.

V/r,

^_^
Was This Post Helpful? 0
  • +
  • -

#4 _WolfShade_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 12-May 14

Re: Weird switch/case issue

Posted 13 May 2014 - 11:32 AM

This is getting weirder.

I did as you suggested, adding a case value="1" and putting it before the case value="0" and defaultcase. No change.

Here's where it gets weirder (and I didn't think of it, last time): The code is within a CFTRY/CFCATCH that is supposed to email me with a dump of the CFCATCH. It's displaying the error in the browser and not sending me an email. Maybe it's because the code is within a function in a .cfc? I don't know. I'll set output to true to see if that changes anything.

V/r,

^_^ (this is my signature.. how do I default emoticons to 'off'?)
Was This Post Helpful? 0
  • +
  • -

#5 _WolfShade_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 12-May 14

Re: Weird switch/case issue

Posted 13 May 2014 - 11:38 AM

Also, I've tried the case values with and without quotes. No difference.

(I'm assuming that after I post x number of times, I'll be able to edit my posts?)

V/r,
Was This Post Helpful? 0
  • +
  • -

#6 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1926
  • View blog
  • Posts: 3,471
  • Joined: 13-January 08

Re: Weird switch/case issue

Posted 13 May 2014 - 11:44 AM

View Post_WolfShade_, on 13 May 2014 - 02:32 PM, said:

This is getting weirder.

I did as you suggested, adding a case value="1" and putting it before the case value="0" and defaultcase. No change.

Here's where it gets weirder (and I didn't think of it, last time): The code is within a CFTRY/CFCATCH that is supposed to email me with a dump of the CFCATCH. It's displaying the error in the browser and not sending me an email. Maybe it's because the code is within a function in a .cfc? I don't know. I'll set output to true to see if that changes anything.


Hm. When you say it's a CFC, is it a component method where you pass values in to it? If so, consider evaluating the server variable first THEN call the method and pass in which server variable you want to work with. It could be that, for some weird reason, the server scope isn't available to the component method. That said, you'd think it would error rather than bring back the incorrect case conclusion...but weirder things have happened.

Quote

^_^ (this is my signature.. how do I default emoticons to 'off'?)


I have no idea.
Was This Post Helpful? 0
  • +
  • -

#7 _WolfShade_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 12-May 14

Re: Weird switch/case issue

Posted 13 May 2014 - 12:29 PM

Odd.. you'd think that the server scope would be available to all CF code. That seems to have fixed it, Craig328.

Still.. it troubles me that A) it chose to run value 0 instead of correct or default, and B) that the try/catch didn't do what it's supposed to do. I'm still trying to understand those two things. And I have a bad feeling that I'll never know.

Thank you for your advice.

V/r,

^_^
Was This Post Helpful? 0
  • +
  • -

#8 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1926
  • View blog
  • Posts: 3,471
  • Joined: 13-January 08

Re: Weird switch/case issue

Posted 13 May 2014 - 12:35 PM

Glad you got it sorted out. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1